package vn.ikame.android.adapter;

import java.util.ArrayList;

import vn.ikame.android.chuyenay.R;
import vn.ikame.android.object.Question;
import vn.ikame.android.object.Var;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class ListQuestionAdapter extends ArrayAdapter<Question>{
    ArrayList<Question> array;
    int resource;
    Context context;
    int pageViewPos;
     
    public ListQuestionAdapter(Context context, int textViewResourceId,
            ArrayList<Question> objects, int pageViewPos) {
        super(context, textViewResourceId, objects);
        this.context = context;
        resource = textViewResourceId;
        array = objects;       
        this.pageViewPos = pageViewPos;
    }   
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View questionView = convertView;
         
        if (questionView == null) {
            questionView = new CustomViewGroup(getContext());
        }
         
        final Question question = array.get(position);
 
        if (question != null) {
            TextView questionContent = ((CustomViewGroup) questionView).questionContent;
            questionContent.setTypeface(Var.tf);
            LinearLayout item = ((CustomViewGroup) questionView).item;
            
            switch (pageViewPos) {
			case Var.CHUYEN_AY:
				item.setBackgroundResource(R.drawable.item);
				break;
			case Var.CHUYEN_ADAM:
				item.setBackgroundResource(R.drawable.male);
				break;
			case Var.CHUYEN_EVA:
				item.setBackgroundResource(R.drawable.female);
				break;
			}
            
        	if(question.getQuestion().length() <= 73){
            	questionContent.setText(question.getQuestion());
            } else {
            	for(int i=73;i>0;i--){
            		if(question.getQuestion().charAt(i) == ' '){
            			questionContent.setText(question.getQuestion().substring(0, i) + "...");
            			break;
            		}
            	}
            }
        }       
        return questionView;
    }   
}